

//
//  UserViewController.m
//  UI14_豆瓣完整版
//
//  Created by dllo on 15/12/31.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "UserViewController.h"
#import "LandingViewController.h"
#import "MyActiveViewController.h"
#import "MyMovieViewController.h"
#import "BYJDataBaseHandle.h"

#import "MovieModel.h"
#import "ActivityModel.h"
#import "UserModel.h"
#import "SDImageCache.h"

@interface UserViewController ()<UITableViewDataSource, UITableViewDelegate, userDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) UIBarButtonItem *item;

@end

@implementation UserViewController

- (void)dealloc {
    [_tableView release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor yellowColor];
    
    self.title = @"我的";
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    
    // 注册重用池
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [_tableView release];
    
    self.item = [[UIBarButtonItem alloc] initWithTitle:@"登陆" style:UIBarButtonItemStyleDone target:self action:@selector(itemAction:)];
    
    self.navigationItem.rightBarButtonItem = _item;
}

// cell的点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        MyActiveViewController *actVc = [[MyActiveViewController alloc] init];
        [self.navigationController pushViewController:actVc animated:YES];
        // 从数据库取值
       NSMutableArray *arr = [[BYJDataBaseHandle sharedDataBaseHandle] selectActivityTable];
        
        actVc.activityArr = arr;
        
        [actVc release];
    }
    if (indexPath.row == 1) {
        MyMovieViewController *movVc = [[MyMovieViewController alloc] init];
        [self.navigationController pushViewController:movVc animated:YES];
        // 从数据库取值
        NSMutableArray *arr = [[BYJDataBaseHandle sharedDataBaseHandle] selectMovieTable];
        
        movVc.movieArr = arr;
        
        [movVc release];
    }

    if (indexPath.row == 2) {
        
        // 清除缓存
        [[SDImageCache sharedImageCache] cleanDisk];
        NSUInteger size = [[SDImageCache sharedImageCache] getSize];
        CGFloat newSize = size / 1024.0 / 1024.0;
        NSLog(@"%.2fMB", newSize);
        
        NSString *str = [NSString stringWithFormat:@"是否清除%.2fMB缓存", newSize];
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:str preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
        [alert addAction:action];
        [alert addAction:action1];
        [self presentViewController:alert animated:YES completion:nil];
        
        // 从数据库中删除数据、
        MovieModel *model1 = [MovieModel movieModel];
        [[BYJDataBaseHandle sharedDataBaseHandle] deleteMovieModel:model1];
        
        ActivityModel *model2 = [ActivityModel activityModel];
        [[BYJDataBaseHandle sharedDataBaseHandle] deleteActivityModel:model2];
 
    }
}

#warning 4.实现协议的方法
- (void)returnTextWithString:(NSString *)string {
    
    _item.title = string;
    
}



// 按钮的点击方法
- (void)itemAction:(UIBarButtonItem *)item {
    LandingViewController *lanVc = [[LandingViewController alloc] init];
    // 查询数据库
    NSMutableArray *arr = [[BYJDataBaseHandle sharedDataBaseHandle] selectUserTable];
    NSMutableArray *modelArr = [NSMutableArray array];
    
    for (UserModel *modle in arr) {
        NSString *nameStr = modle.name;
        [modelArr addObject:nameStr];
    }
    
    if ([_item.title  isEqual: @"注销"]) {
        
        // 删除数据库的值
        UserModel *model = [UserModel userModel];
        [[BYJDataBaseHandle sharedDataBaseHandle] deleteUserModel:model];
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否注销" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
        
        [alert addAction:action];
        [alert addAction:action1];
        
        [self presentViewController:alert animated:YES completion:nil];
        }
    
    _item.title = @"登陆";
    
    
     lanVc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentViewController:lanVc animated:YES completion:nil];
    
#warning 5.设置代理人
    lanVc.delegate = self;
}

// 行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}

// 行宽
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

// cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 200, 40)];
    label.backgroundColor = [UIColor clearColor];
    [cell addSubview:label];
    [label release];
    
    if (indexPath.row == 0) {
        label.text = @"我的活动";
    }
    
    if (indexPath.row == 1) {
        label.text = @"我的电影";
    }
    
    if (indexPath.row == 2) {
        label.text = @"清除缓存";
    }
    
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
